package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.ClassicWallpaperActivity;
import flc.ast.activity.HotWallpaperActivity;
import flc.ast.activity.WallpaperActivity;
import flc.ast.adapter.TabWallpaperAdapter;
import flc.ast.databinding.FragmentWallpaperBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link WallpaperFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WallpaperFragment extends BaseNoModelFragment<FragmentWallpaperBinding> {

    private TabWallpaperAdapter hotWallpaperAdapter;
    private TabWallpaperAdapter classicWallpaperAdapter;


    @Override
    protected int onCreate() {
        return R.layout.fragment_wallpaper;
    }

    @Override
    protected void initView() {
        mDataBinding.ivHotWallpaperMore.setOnClickListener(this);
        mDataBinding.ivClassicWallpaperMore.setOnClickListener(this);
        mDataBinding.rvHotWallpaperList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        hotWallpaperAdapter=new TabWallpaperAdapter();
        mDataBinding.rvHotWallpaperList.setAdapter(hotWallpaperAdapter);
        hotWallpaperAdapter.setOnItemClickListener(this);

        mDataBinding.rvClassicWallpaperList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        classicWallpaperAdapter=new TabWallpaperAdapter();
        mDataBinding.rvClassicWallpaperList.setAdapter(classicWallpaperAdapter);
        classicWallpaperAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivHotWallpaperMore://热门推荐
                startActivity(HotWallpaperActivity.class);
                break;
            case R.id.ivClassicWallpaperMore://经典壁纸
                startActivity(ClassicWallpaperActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        WallpaperActivity.listWallpaper = (List<StkResBean>) adapter.getData();
        WallpaperActivity.selPosition=position;
        startActivity(WallpaperActivity.class);
    }

    @Override
    protected void initData() {
        getHotWallpaperData();
        getClassicWallpaperData();
    }

    private void getHotWallpaperData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_WALLPAPER_DATA, StkResApi.createParamMap(0, 5), true, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotWallpaperAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getClassicWallpaperData() {
        StkResApi.getTagResourceList(this, AppConfig.CLASSIC_WALLPAPER_DATA, StkResApi.createParamMap(0, 5), true, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        classicWallpaperAdapter.setList(result);
                    }
                }
            }
        });
    }

}